<?php

class GamesController extends BaseController {

	public function listGames()
	{
		$result = Game::orderBy('data')->get();

		return View::make('gameManagement/list')->with(compact('result'));
	}

	public function deleteGame()
	{
		$id = Input::get('gameId');
		DB::table('teams')->where('gameid', '=', $id)->delete();
		DB::table('games')->where('id', '=', $id)->delete();
		$games = DB::table('games')->get();

		return Response::json( array('games' => $games));
	}

	public function activateGame()
	{
		$id = Input::get('gameId');
		DB::table('games')->update(array('activo' => false));
		DB::table('games')->where('id', '=', $id)->update(array('activo' => true));
		$games = DB::table('games')->get();

		return Response::json( array('games' => $games));
	}

	public function newGame()
	{
		return View::make('gameManagement/newGame');
	}

	public function createNewGame()
	{
		$date = date('Y-m-d');
		$name 	 = Input::has('gameName') 	 ? Input::get('gameName') 	: '';
		$company = Input::has('gameCompany') ? Input::get('gameCompany'): '';
		$date 	 = Input::has('gameDate') 	 ? Input::get('gameDate')	: $date;
		$size 	 = Input::get('numTeams');

		$activeGame = Game::where('activo', '=', true)->first();
		if(isset($activeGame))
		{
			$activeGame->activo = false;
			$activeGame->save();
		}

		$game = new Game();
		$game->nome = $name;
		$game->empresa = $company;
		$game->data = $date;
		$game->numEquipas = $size;
		$game->activo = true;
		$game->save();

		$teams = "";

		return View::make('gameManagement/gameTeams')->with(compact('game'));
	}

	public function editGame($id)
	{
		$game = Game::where('id', '=', $id)->first();
		$teams = Team::where('gameid', '=', $id)->get();

		return View::make('gameManagement/editGameTeams')->with(compact('game'))->with(compact('teams'));
	}

	public function setTeams()
	{

		#get data from form:
		$teams = Input::get('teams');
		$game = Input::get('game');
		//Log::info("teams: ".$teams." GAME: ".$game);
		$result = 0;

		for ($i=0; $i < $game['numEquipas']; $i++)
		{ 
			$t = new Team();
			$t->teamid = $i+1;
			$t->gameid = $game['id'];
			$t->nome = $teams[$i];
			$t->save();

			$result++;
		}

		if($result == $game['numEquipas'])
		{
			return Response::json(array('result' => 'ok'));
		}
		else
		{
			#clear previous data from the game:
			DB::table('games')->where('id', '=', $game['id'])->delete();
			DB::table('teams')->where('gameid', '=', $game['id'])->delete();

			return Response::json(array('result' => 'ko'));
		}
	}

	public function editTeams()
	{

		#get data from form:
		$teams = Input::get('teams');
		$game = Input::get('game');
		//Log::info("teams: ".$teams." GAME: ".$game);
		$result = 0;

		for ($i=0; $i < $game['numEquipas']; $i++)
		{ 
			$team = Team::where('gameid', '=', $game['id'])->where('teamid', '=', $i+1)->first();
			if(isset($team))
			{
				$team->nome = $teams[$i];
				$team->save();
			}
				
			$result++;
		}

		if($result == $game['numEquipas'])
		{
			return Response::json(array('result' => 'ok'));
		}
		else
		{
			#clear previous data from the game:
			DB::table('games')->where('id', '=', $game['id'])->delete();
			DB::table('teams')->where('gameid', '=', $game['id'])->delete();

			return Response::json(array('result' => 'ko'));
		}
	}
}